<?php

class Utils {
	
	const 
		tmp_dir = TEMP_DIR,
		ext_dir = EXTERNAL_PATH,
		utils_dir = "Utils/";
	
	protected  
		$_top,
		$_parent;
	
	
	public function __construct($top,$parent)
	{
		$this->_top =& $top;
		$this->_parent =& $parent;
	}
	
	public function prepareValueForSQL($value)
	{
		// TODO  вставить проверку на MAGIC_QUOTES
		$value = str_replace("\\","\\\\",$value);
		$value = str_replace("'","\'",$value);
		return $value;
	}
	
	public function file_name_validator($file_name)
	{
		$chars  = "qwertyuiopasdfghjklzxcvbnm";
		$chars .= "QWERTYUIOPASDFGHJKLZXCVBNM";
		$chars .= "0123456789\.-_";
		if (ereg("[^$chars]",$file_name)) 
			return false;
		else 
			return true;
	}
	
	public function fixDirPath(&$dir_name)
	{
		$dir_name = str_replace("\\","/",$dir_name);
		while (strpos($dir_name,'//')!==false) {
			$dir_name = str_replace("//","/",$dir_name);
		}
		if (!ereg("/$",$dir_name)) $dir_name .= "/";
		return $dir_name;
	}

	
	/**
	 * Возвращает указатель на класс File
	 *
	 * @param string $file_path
	 * @return File
	 */
	public function File($file_path)
	{
		require_once $this->_top->getApiPath().self::utils_dir.'File.inc';
		return new File($this->_top,$this,$file_path);
	}
	
	/**
	 * Returns new instance of Image object
	 *
	 * @param String $file_path
	 * @return Image
	 */
	public function Image($file_path)
	{
		require_once $this->_top->getApiPath().self::utils_dir.'File.inc';
		return new Image($this->_top,$this,$file_path);
	}
	
	public function PushError($sys_message,$user_message)
	{
		//if (!class_exists("PushError")) {
			require_once $this->_top->getApiPath().self::utils_dir.'PushError.inc';
		//}
		return new PushError($this->_top,$this,$sys_message,$user_message);
	}
	
	public function MindTempFile($ext)
	{
		do {
			$t_inf = pathinfo (tempnam(self::tmp_dir,"tmp_"));
			$tmp_name = self::tmp_dir.ereg_replace("^(.+)\..+$","\\1",$t_inf['basename']).".$ext";
		} while (file_exists($tmp_name));
		return $tmp_name;
	}
	
	public function ImageDownloader($resourse = "")
	{
		if (!class_exists("ImageDownloader")) require_once $this->_top->getApiPath().self::utils_dir.'File.inc';
		return new ImageDownloader($this->_top,$this,$resourse);
	}
	
	/**
	 * Возвращает xml файли в виде объекта 
	 *
	 * @param String $file
	 * @return DOMIT_Document
	 */
	public function ParseXML($file)
	{
		require_once /*self::ext_dir.*/"domit/xml_domit_include.php";
		$xmlDoc = new DOMIT_Document();
		if (!$xmlDoc->loadXML($file)) {
			$this->PushError('Cant read XML file', 'Системная ошибка');
		}/* else {
			echo 'Parse OK!';
			echo $xmlDoc->toString(true);
		}*/
		
		return $xmlDoc->documentElement;
	}
	
	public function Template()
	{
		require_once self::ext_dir.'Smarty/Smarty.class.php';
		return new Smarty();
	}
	
	/**
	 * Возвращает указатель на класс Registry
	 *
	 * @return Registry
	 */
	public function Registry() {
		if (!class_exists("Registry")) require_once $this->_top->getApiPath().self::utils_dir.'Registry.inc';
		return new Registry($this->_top,$this);
	}
	
	public function json_decoder($json_string) {
		Zend_Json::$useBuiltinEncoderDecoder = true;
		return Zend_Json::decode($json_string);
	}
	
	public function emailValidator($email) {
		return ereg("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$",$email); 
	}
	
}

?>